package cn.wolfcode.dao.impl;


import cn.wolfcode.dao.IStudentDao;
import cn.wolfcode.domain.Student;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Repository("IStudentDao")
@Scope("prototype")
public class StudentDaoimpl implements IStudentDao {
    @Autowired
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource){
        this.dataSource = dataSource;
    }
    @Override
    public void save(String username, String password) throws Exception {
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("insert into student(username,password)"+
                "values (?,?)");
        ps.setString(1,username);
        ps.setString(2,password);
        ps.executeUpdate();
    }

    @Override
    public void delect(long id) throws Exception {
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("delete from student where id=?");
        ps.setString(1, String.valueOf(id));
        ps.executeUpdate();
    }

    @Override
    public void update(Long id, String username, String password) throws Exception {
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("update student set username=?,password=? where id=?");
        ps.setString(1,username);
        ps.setString(2,password);
        ps.setString(3, String.valueOf(id));
        ps.executeUpdate();
    }

    @Override
    public List<Student> select() throws Exception {
        List<Student> students = new ArrayList<>();

        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("select * from student");
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()){
            Student student = new Student();
            student.setId(resultSet.getLong(1));
            student.setUsername(resultSet.getString(2));
            student.setPassword(resultSet.getString(3));

            students.add(student);
        }
        System.out.println(students);
        return students;

    }

    @Override
    public Student selectid(Long id) throws Exception {
        Student student = new Student();
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("select * from student where id=?");
        ps.setLong(1,id);
        ResultSet resultSet = ps.executeQuery();
        if(resultSet.next()){
            student.setId(resultSet.getLong(1));
            student.setUsername(resultSet.getString(2));
            student.setPassword(resultSet.getString(3));
        }
        System.out.println(student);
        return student;
    }
}
